package com.agile.major.web.rest;

import com.agile.major.domain.CommonCourseOption;
import com.agile.major.service.CommonCourseOptionService;
import com.agile.major.web.rest.util.HeaderUtil;
import com.agile.major.web.rest.util.PaginationUtil;
import com.codahale.metrics.annotation.Timed;
import io.github.jhipster.web.util.ResponseUtil;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.Optional;

/**
 * REST controller for managing CommonCourseOption.
 */
@RestController
@RequestMapping("/api")
public class CommonCourseOptionResource {

    private final Logger log = LoggerFactory.getLogger(CommonCourseOptionResource.class);

    private static final String ENTITY_NAME = "commonCourseOption";

    private final CommonCourseOptionService commonCourseOptionService;

    public CommonCourseOptionResource(CommonCourseOptionService commonCourseOptionService) {
        this.commonCourseOptionService = commonCourseOptionService;
    }

    /**
     * POST  /common-course-options : Create a new commonCourseOption.
     *
     * @param commonCourseOption the commonCourseOption to create
     * @return the ResponseEntity with status 201 (Created) and with body the new commonCourseOption, or with status 400 (Bad Request) if the commonCourseOption has already an ID
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PostMapping("/common-course-options")
    @Timed
    public ResponseEntity<CommonCourseOption> createCommonCourseOption(@RequestBody CommonCourseOption commonCourseOption) throws URISyntaxException {
        log.debug("REST request to save CommonCourseOption : {}", commonCourseOption);
        if (commonCourseOption.getId() != null) {
            return ResponseEntity.badRequest().headers(HeaderUtil.createFailureAlert(ENTITY_NAME, "idexists", "A new commonCourseOption cannot already have an ID")).body(null);
        }
        CommonCourseOption result = commonCourseOptionService.save(commonCourseOption);
        return ResponseEntity.created(new URI("/api/common-course-options/" + result.getId()))
            .headers(HeaderUtil.createEntityCreationAlert(ENTITY_NAME, result.getId().toString()))
            .body(result);
    }

    /**
     * PUT  /common-course-options : Updates an existing commonCourseOption.
     *
     * @param commonCourseOption the commonCourseOption to update
     * @return the ResponseEntity with status 200 (OK) and with body the updated commonCourseOption,
     * or with status 400 (Bad Request) if the commonCourseOption is not valid,
     * or with status 500 (Internal Server Error) if the commonCourseOption couldn't be updated
     * @throws URISyntaxException if the Location URI syntax is incorrect
     */
    @PutMapping("/common-course-options")
    @Timed
    public ResponseEntity<CommonCourseOption> updateCommonCourseOption(@RequestBody CommonCourseOption commonCourseOption) throws URISyntaxException {
        log.debug("REST request to update CommonCourseOption : {}", commonCourseOption);
        if (commonCourseOption.getId() == null) {
            return createCommonCourseOption(commonCourseOption);
        }
        CommonCourseOption result = commonCourseOptionService.save(commonCourseOption);
        return ResponseEntity.ok()
            .headers(HeaderUtil.createEntityUpdateAlert(ENTITY_NAME, commonCourseOption.getId().toString()))
            .body(result);
    }

    /**
     * GET  /common-course-options : get all the commonCourseOptions.
     *
     * @param pageable the pagination information
     * @return the ResponseEntity with status 200 (OK) and the list of commonCourseOptions in body
     */
    @GetMapping("/common-course-options")
    @Timed
    public ResponseEntity<List<CommonCourseOption>> getAllCommonCourseOptions(@ApiParam Pageable pageable) {
        log.debug("REST request to get a page of CommonCourseOptions");
        Page<CommonCourseOption> page = commonCourseOptionService.findAll(pageable);
        HttpHeaders headers = PaginationUtil.generatePaginationHttpHeaders(page, "/api/common-course-options");
        return new ResponseEntity<>(page.getContent(), headers, HttpStatus.OK);
    }

    /**
     * GET  /common-course-options/:id : get the "id" commonCourseOption.
     *
     * @param id the id of the commonCourseOption to retrieve
     * @return the ResponseEntity with status 200 (OK) and with body the commonCourseOption, or with status 404 (Not Found)
     */
    @GetMapping("/common-course-options/{id}")
    @Timed
    public ResponseEntity<CommonCourseOption> getCommonCourseOption(@PathVariable Long id) {
        log.debug("REST request to get CommonCourseOption : {}", id);
        CommonCourseOption commonCourseOption = commonCourseOptionService.findOne(id);
        return ResponseUtil.wrapOrNotFound(Optional.ofNullable(commonCourseOption));
    }

    /**
     * DELETE  /common-course-options/:id : delete the "id" commonCourseOption.
     *
     * @param id the id of the commonCourseOption to delete
     * @return the ResponseEntity with status 200 (OK)
     */
    @DeleteMapping("/common-course-options/{id}")
    @Timed
    public ResponseEntity<Void> deleteCommonCourseOption(@PathVariable Long id) {
        log.debug("REST request to delete CommonCourseOption : {}", id);
        commonCourseOptionService.delete(id);
        return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build();
    }
    @ApiOperation(value = "获取已发布状态的通识通修课程")
    @GetMapping("/common-course-options/commonStatus")
    @Timed
    public ResponseEntity<List<CommonCourseOption>> getCommonCourseOptions(@RequestParam("commonStatus") String commonStatus) {
        try {
            List<CommonCourseOption> commonCourseOptionsBy = commonCourseOptionService.getCoursesByoptionStatus(commonStatus);
            return ResponseEntity.ok(commonCourseOptionsBy);
        } catch (Exception e) {
            e.printStackTrace();
            return new ResponseEntity<>(HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
